package com.pureut.storage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.AllotManagement;
import com.pureut.storage.domain.AllotManagementDetail;
import com.pureut.storage.dto.AllotManagementDetailDto;
import com.pureut.storage.mapper.AllotManagementDetailMapper;
import com.pureut.storage.mapper.AllotManagementMapper;
import com.pureut.storage.service.AllotManagementDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 调拨管理明细实现层
 *
 * @author: CM
 * @date: 2023/2/14  14:12
 */
@Service
@RequiredArgsConstructor
public class AllotManagementDetailServiceImpl extends ServiceImpl<AllotManagementDetailMapper, AllotManagementDetail> implements AllotManagementDetailService {

    @NonNull
    private AllotManagementDetailMapper allotManagementDetailMapper;

    @NonNull
    private AllotManagementMapper allotManagementMapper;

    @NonNull
    private RemoteUnitService remoteUnitService;

    /**
     * 调拨单明细列表
     *
     * @param docNum
     * @return
     */
    @Override
    public TableDataInfo<AllotManagementDetailDto> getList(String docNum, PageQuery pageQuery) {

        //获取单据状态字典数据
        List<SysDictData> statusArray = CacheUtils.get(CacheNames.SYS_DICT, "allot_management_detail_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        Page<AllotManagementDetailDto> allotManagementDetailDtoList = allotManagementDetailMapper.getAllotManagementDetailList(pageQuery.build(), docNum);

        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        for (AllotManagementDetailDto allotManagementDetailDto : allotManagementDetailDtoList.getRecords()) {

            allotManagementDetailDto.setMaterialStatusDict(allotManagementDetailDto.getMaterialStatus());
            allotManagementDetailDto.setMaterialStatus(statusMap.get(allotManagementDetailDto.getMaterialStatus()));
            if (allotManagementDetailDto.getMaterialUnit() != null) {
                allotManagementDetailDto.setMaterialUnitDict(unitByCodeToMap.get(String.valueOf(allotManagementDetailDto.getMaterialUnit())).getUnitName());
            }

            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + allotManagementDetailDto.getTenantId(), String.valueOf(allotManagementDetailDto.getMaterialId()));
            allotManagementDetailDto.setAuxiliaryAttribute(statsArray);
        }

        return TableDataInfo.build(allotManagementDetailDtoList);
    }
}
